package b1.scenes.fight
{
	import b1.conifg.*;
	import b1.controls.basic.BasicMessage;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.BFont;
	
	import com.greensock.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.utils.Timer;

	public class ProgressBar extends Sprite
	{
		public var progressIcon:Sprite;
		private var road:Sprite;
		private var timer:Timer;
		private var speed:Number;
		public var reachRightEdgeFlag:Boolean;
		private var owner:Animal;
		public var stopFlag:Boolean;
//		private var lastx:Number;
		private const barWidth:int = 100;
		private const barHeight:int = 20;
		private var iconDizzy:MovieClip;
		private var iconIce:Sprite;
//		private var imgRatio:Number;
		private var lastFilterArray:Array;
		private const iconDiagConst:Number = 40;
		
		private var tip:BasicMessage;
		
		public function ProgressBar(speed:Number, owner:Animal)
		{
			super();
			//底层
			var Road:Class = ResourceHelper.getInstance().getClassByName("Road");
			road = new Road();
			addChild(road);
			road.height = barHeight;
			road.width = barWidth;
			//上层
			var ownerAnimalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[owner.animalId];
			var ProgressIconStr:String = "SmallAnimal" + ownerAnimalInfo.animalIndex.toString();
			var ProgressIcon:Class = ResourceHelper.getInstance().getClassByName(ProgressIconStr);
			if (ProgressIcon == null)
				ProgressIcon = ResourceHelper.getInstance().getClassByName("SmallAnimal0");
			progressIcon = new ProgressIcon();
			var iconDiag:Number = Math.sqrt(progressIcon.width * progressIcon.width + progressIcon.height * progressIcon.height);
			var imgRatio:Number = iconDiagConst / iconDiag;
			progressIcon.height = imgRatio * progressIcon.height;
			progressIcon.width = imgRatio * progressIcon.width;
			progressIcon.y = -progressIcon.height + barHeight * 1 / 4;
			addChild(progressIcon);

			this.speed = ((speed - 20) * 0.4 + 20) * 0.1;

			this.owner = owner;
			reachRightEdgeFlag = false;
			stopFlag = true;
			lastFilterArray = null;
			//加载时钟
			timer = new Timer(50);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER, onTick);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		//设置进度条的速度
		public function setSpeed(speed:Number):void
		{
			this.speed = ((speed - 20) * 0.4 + 20) * 0.1;

		}
		
		//获取进度条的速度
		public function getSpeed():Number
		{
			return this.speed;
		}
		
		//图标回到开始位置
		private function resetProgressIcon():void
		{
			reachRightEdgeFlag = false;
//			lastx = 0;
			progressIcon.x = 0;
			stopGlow();
			if (null != lastFilterArray)
			{
				progressIcon.filters = [new ColorMatrixFilter(this.lastFilterArray)];
			}
		}

		public function stop():void
		{
			this.stopFlag = true;
		}
		
		public function resume():void
		{
			this.stopFlag = false;
			if (reachRightEdgeFlag)
			{
				resetProgressIcon();
			}
		}
		
		public function onTick(event:Event):void
		{
			//停止
			if (this.stopFlag)
				return;
			//停止运动一段时间
			if (owner.timeToStopDizzy > 0)
			{
				if (ServerHelper.getInstance().now() < owner.timeToStopDizzy)
				{
					this.iconDizzy.x = progressIcon.x + 5;
					return;
				}
				else
				{
					owner.stopDizzy(); 
				}
			}
			//速度减慢一段时间
			if (this.owner.timeToStopSlow > 0 )
			{
				if (ServerHelper.getInstance().now() < owner.timeToStopSlow)
				{
					this.iconIce.x = this.progressIcon.x;
//					this.owner.setAnimalIceImg();
				}
				else
				{
					this.owner.stopSlow();
				}
			}
			//重复到达？
			if (reachRightEdgeFlag)
			{
				resetProgressIcon();
			}
			//没有到达右端点
			else if (progressIcon.x < road.x + road.width - progressIcon.width * 0.6)
			{
				var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[owner.animalId];
				if (animalInfo.vitalityPoint > 0)
				{
					progressIcon.x += this.speed / 2;
				}
			}
			//到达右端点
			else
			{
				owner.onAction();
				glow();
				reachRightEdgeFlag = true;
			}
		}
		
		
		public function glow():void
		{		
			var glowFilter:GlowFilter = new GlowFilter(0xffff00, 1, 4, 4, 2, BitmapFilterQuality.MEDIUM, false, false);
			this.progressIcon.filters = new Array(glowFilter);
		}
		
		public function stopGlow():void
		{
			this.progressIcon.filters = null;
		}
		
		public function addIconDizzyMc(animalIndex:int):void
		{
			var AnimalDizzy:Class = ResourceHelper.getInstance().getClassByName("AnimalDizzy");
			iconDizzy = new AnimalDizzy();
			this.addChild(iconDizzy);
			iconDizzy.width = 26;
			iconDizzy.scaleY = iconDizzy.scaleX;
			iconDizzy.y = this.progressIcon.y - 10;
			iconDizzy.x = this.progressIcon.x + 5;
		}
		
		public function removeIconDizzyMc():void
		{
			removeChild(iconDizzy);
		}
		
		public function addIconIce(animalIndex:int, iconIceAlpha:Number):void
		{
			var AnimalIce:Class = ResourceHelper.getInstance().getClassByName("Ice" + 3);//animalIndex.toString());
			iconIce = new AnimalIce();
			this.addChild(iconIce);
			iconIce.width = this.progressIcon.width;
			iconIce.height = this.progressIcon.height;
			iconIce.alpha = iconIceAlpha;
			iconIce.y = this.progressIcon.y;
			iconIce.x = this.progressIcon.x;
		}
		
		//去掉冰图片
		public function removeIconIce():void
		{
			removeChild(iconIce);
		}
		
		//图标filter加载
		public function addIconFilter(redFilter:Array):void
		{
			this.lastFilterArray = redFilter;
			this.progressIcon.filters = [new ColorMatrixFilter(BFont.redFilter)];
		}
		
		//图标filter去除
		public function removeIconFilters():void
		{
			this.progressIcon.filters = null;
			this.lastFilterArray = null;
		}
		
		//显示“进度”文本
		public function onMouseOver(event:MouseEvent):void
		{
			var tipString:String = "进度";
			var tipWidth:int = tipString.length * 20;
			tip = new BasicMessage(tipWidth, 25, tipString, 0x008000);
			if (this.owner.leftFlag)
			{
				tip.x = this.barWidth + 5;
			}
			else
			{
				tip.x = - tipWidth - 5;
			}
			tip.y = -5;
			addChild(tip);
			
		}
		
		//去掉“进度”文本
		public function onMouseOut(event:MouseEvent):void
		{
			if (null !=  this.tip)
        	{
        		this.removeChild(tip);
        		tip = null;
        	}
		}
		
		//去除自身
		public function destory():void
		{
			if (timer != null)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onTick);
				timer = null;
			}
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}

	}
}